package com.ymttest.database.mongo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.mongodb.MongoClientURI;
import com.mongodb.WriteResult;
import com.mysql.fabric.xmlrpc.base.Value;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/************************************************************************************
 * @File name :ProductBrandInfoWapper.java
 * @Author :ligaoge
 * @Date : 2016年8月23日
 * @Copyright : 洋码头 商品合库 新库
 ************************************************************************************/
public class ProductsWapper {
	MongoDBDriver mdd;

	public ProductsWapper() throws NumberFormatException, Exception {
		MongoClientURI uri = new MongoClientURI(EnvSetup.getSystemUrl("set5_mongo_YmtProducts"));
		mdd = new MongoDBDriver(uri);
	}

	/**
	 * 按BrandId查询出一条记录
	 * 
	 * @param BrandId
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getActivityProductsByInaid(Integer inaid) throws Exception {
		mdd.selectCollection("ActivityProducts");
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"inaid\":" + inaid + "}", 1);
		try {
			if (list.size() > 0) {
				return list;
			}
		} catch (Exception e) {
			Logger.debug("err data inaid:" + inaid);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 用inaid更新plimit
	 * 
	 * @param inaid
	 * @param plimit
	 * @return
	 * @throws Exception
	 */
	public Integer updatePlimitByInaid(Integer inaid, Integer plimit) throws Exception {
		mdd.selectCollection("ActivityProducts");
		WriteResult wr = mdd.update("{\"inaid\":" + inaid + "}", "{\"plimit\":" + plimit + "}",true,false);
		return wr.getN();
	}

	/**
	 * 用inaid更新alimit
	 * 
	 * @param inaid
	 * @param alimit
	 * @return
	 * @throws Exception
	 */
	public Integer updateAlimitByInaid(Integer inaid, Integer alimit) throws Exception {
		mdd.selectCollection("ActivityProducts");
		WriteResult wr = mdd.update("{\"inaid\":" + inaid + "}", "{\"alimit\":" + alimit + "}",true,false);
		return wr.getN();
	}

	/**
	 * 更新Products
	 * 
	 * @param spid
	 * @param key
	 * @param value
	 * @return
	 * @throws Exception
	 */
	public Integer updateProductsBySpid(String spid, String key, Object value) throws Exception {
		mdd.selectCollection("Products");
		WriteResult wr = null;
		if(value==null){
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':" + value + "}",true,false);
			return wr.getN();
		}
		else if (value.getClass().getName().contains("String")) {
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':'" + value + "'}",true,false);
			return wr.getN();
		}else if (value.getClass().getName().contains("Date")) {
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':{$date:"+((Date)value).getTime()+"}}",true,false);
			return wr.getN();
		}else if(value.getClass().getName().contains("List")){
			value=FormatUtil.beanToGSONString(value);
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':" + value + "}",true,false);
			
			return wr.getN();
		}else{
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':" + value + "}",true,false);
			return wr.getN();
		}
		
	}

	/**
	 * spid获取Products
	 * 
	 * @param spid
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductsBySpid(String spid) throws Exception {
		mdd.selectCollection("Products");
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":'" + spid + "'}", 10);
		try {
			if (list.size() > 0) {
				return list;
			}
		} catch (Exception e) {
			Logger.debug("err data spid:" + spid);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 更新ProductDescExtra
	 * 
	 * @param spid
	 * @param key
	 * @param value
	 * @return
	 * @throws Exception
	 */
	public Integer updateProductDescExtraBySpid(String spid, String key, Object value) throws Exception {
		mdd.selectCollection("ProductDescExtra");
		WriteResult wr = null;
		if(value==null){
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':" + value + "}",true,false);
			return wr.getN();
		}
		else if (value.getClass().getName().contains("String")) {
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':'" + value + "'}",true,false);
			return wr.getN();
		}else if (value.getClass().getName().contains("Date")) {
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':{$date:"+((Date)value).getTime()+"}}",true,false);
			return wr.getN();
		}else if(value.getClass().getName().contains("List")){
			value=FormatUtil.beanToGSONString(value);
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':" + value + "}",true,false);
			
			return wr.getN();
		}else{
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':" + value + "}",true,false);
			return wr.getN();
		}
		
	}

	/**
	 * spid获取ProductDescExtra
	 * 
	 * @param spid
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getProductDescExtraBySpid(String spid) throws Exception {
		mdd.selectCollection("ProductDescExtra");
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":'" + spid + "'}", 10);
		try {
			if (list.size() > 0) {
				return list;
			}
		} catch (Exception e) {
			Logger.debug("err data spid:" + spid);
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 更新ProductDescExtra
	 * 
	 * @param spid
	 * @param key
	 * @param value
	 * @return
	 * @throws Exception
	 */
	public Integer updateFristCatalogsBySpid(String spid, String key, Object value) throws Exception {
		mdd.selectCollection("Catalogs");
		WriteResult wr = null;
		if(value==null){
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':" + value + "}",true,false);
			return wr.getN();
		}
		else if (value.getClass().getName().contains("String")) {
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':'" + value + "'}",true,false);
			return wr.getN();
		}else if (value.getClass().getName().contains("Date")) {
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':{$date:"+((Date)value).getTime()+"}}",true,false);
			return wr.getN();
		}else if(value.getClass().getName().contains("List")){
			value=FormatUtil.beanToGSONString(value);
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':" + value + "}",true,false);
			
			return wr.getN();
		}else{
			wr = mdd.update("{'spid':'" + spid + "'}", "{'" + key + "':" + value + "}",true,false);
			return wr.getN();
		}
		
	}
	/**
	 * 更新updateCatalogsByCid
	 * 
	 * @param cid
	 * @param key
	 * @param value
	 * @return
	 * @throws Exception
	 */
	public Integer updateCatalogsByCid(String cid, String key, Object value) throws Exception {
		mdd.selectCollection("Catalogs");
		WriteResult wr = null;
		if(value==null){
			wr = mdd.update("{'cid':'" + cid + "'}", "{'" + key + "':" + value + "}",true,false);
			return wr.getN();
		}
		else if (value.getClass().getName().contains("String")) {
			wr = mdd.update("{'cid':'" + cid + "'}", "{'" + key + "':'" + value + "'}",true,false);
			return wr.getN();
		}else if (value.getClass().getName().contains("Date")) {
			wr = mdd.update("{'cid':'" + cid + "'}", "{'" + key + "':{$date:"+((Date)value).getTime()+"}}",true,false);
			return wr.getN();
		}else if(value.getClass().getName().contains("List")){
			value=FormatUtil.beanToGSONString(value);
			wr = mdd.update("{'cid':'" + cid + "'}", "{'" + key + "':" + value + "}",true,false);
			return wr.getN();
		}else{
			wr = mdd.update("{'cid':'" + cid + "'}", "{'" + key + "':" + value + "}",true,false);
			return wr.getN();
		}
		
	}

	/**
	 * spid获取ProductDescExtra
	 * 
	 * @param spid
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getCatalogsBySpid(String spid) throws Exception {
		mdd.selectCollection("Catalogs");
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"spid\":'" + spid + "'}", 10);
		try {
			if (list.size() > 0) {
				return list;
			}
		} catch (Exception e) {
			Logger.debug("err data spid:" + spid);
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String args[]) throws Exception {
		ProductsWapper pw = new ProductsWapper();
		String spid = "d34e1e0e-7f44-4f83-82b9-26a21d916dc1";
		pw.updateFristCatalogsBySpid(spid, "test",111);
		System.out.println(pw.getCatalogsBySpid(spid));
	}
}
